/**
 * date:2010-09-29
 */
package com.weipu.common.facade.model;

import java.io.Serializable;
import java.util.ArrayList;

import org.json.JSONException;
import org.json.JSONObject;

import com.weipu.common.constants.ServerConstant;
import com.weipu.common.constants.ServerConstant.Wish;

/** 
 * @Description 收藏夹model
 * @version [版本号, 2012-10-29]
 * @see [相关类/方法]
 * @since [产品/模块版本]
 * 
 * @author yw66009
 */
public class WishModel implements Serializable
{
    /**
     * 注释内容
     */
    private static final long serialVersionUID = -5838298406879449801L;
    
    private String UserName;
    
    private int Sku;
    
    private ProductInfo ProductInfo;
    
    private String currencyCode;//货币简写
    
    private String currencyCodeSymbol;//货币符号
    
    public static class ProductInfo implements Serializable
    {
        
        private static final long serialVersionUID = -6093424891631509164L;
        
        private String ShortHeadLine;
        
        private double Price;
        
        private double ListPrice;
        
        private double ProductRating;
        
        private int ReviewCount;
        
        private String SlideShowPictureThumbURL;
        
        private int freeShipping;
        
        private String webUrl;
        
        public int getFreeShipping()
        {
            return freeShipping;
        }
        
        public void setFreeShipping(int freeShipping)
        {
            this.freeShipping = freeShipping;
        }
        
        public String getWebUrl()
        {
            return webUrl;
        }
        
        public void setWebUrl(String webUrl)
        {
            this.webUrl = webUrl;
        }
        
        public double getListPrice()
        {
            return ListPrice;
        }
        
        public void setListPrice(double listPrice)
        {
            ListPrice = listPrice;
        }
        
        public double getProductRating()
        {
            return ProductRating;
        }
        
        public void setProductRating(double productRating)
        {
            ProductRating = productRating;
        }
        
        public int getReviewCount()
        {
            return ReviewCount;
        }
        
        public void setReviewCount(int reviewCount)
        {
            ReviewCount = reviewCount;
        }
        
        public String getShortHeadLine()
        {
            return ShortHeadLine;
        }
        
        public void setShortHeadLine(String shortHeadLine)
        {
            ShortHeadLine = shortHeadLine;
        }
        
        public double getPrice()
        {
            return Price;
        }
        
        public void setPrice(double price)
        {
            Price = price;
        }
        
        public String getSlideShowPictureThumbURL()
        {
            return SlideShowPictureThumbURL;
        }
        
        public void setSlideShowPictureThumbURL(String slideShowPictureThumbURL)
        {
            SlideShowPictureThumbURL = slideShowPictureThumbURL;
        }
        
    }
    
    
    
    public String getCurrencyCode() {
		return currencyCode;
	}

	public void setCurrencyCode(String currencyCode) {
		this.currencyCode = currencyCode;
	}

	public String getCurrencyCodeSymbol() {
		return currencyCodeSymbol;
	}

	public void setCurrencyCodeSymbol(String currencyCodeSymbol) {
		this.currencyCodeSymbol = currencyCodeSymbol;
	}

	public String getUserName()
    {
        return UserName;
    }
    
    public void setUserName(String userName)
    {
        UserName = userName;
    }
    
    public int getSku()
    {
        return Sku;
    }
    
    public void setSku(int sku)
    {
        Sku = sku;
    }
    
    public ProductInfo getProductInfo()
    {
        return ProductInfo;
    }
    
    public void setProductInfo(ProductInfo productInfo)
    {
        ProductInfo = productInfo;
    }
    
    public static long getSerialversionuid()
    {
        return serialVersionUID;
    }
    
    /**
      * 对象转JSON
      * @return  [参数说明]
      * @return JSONObject [返回类型说明]
      * @exception throws [违例类型] [违例说明]
      * @see [类、类#方法、类#成员]
      */
    public JSONObject tojson()
    {
        JSONObject json = new JSONObject();
        try
        {
            if (null != UserName)
            {
                json.put(Wish.CONSTANT_USERNAME, UserName);
            }
            if (0 != Sku)
            {
                json.put(Wish.CONSTANT_SKU, Sku);
            }
            
            json.put(Wish.CONSTANT_PRICE, ProductInfo.Price);
            
            if (null != ProductInfo.ShortHeadLine)
            {
                json.put(Wish.CONSTANT_SLIDESHOWPICTURETHUMBURL, ProductInfo.ShortHeadLine);
            }
            if (-1 != ProductInfo.ReviewCount)
            {
                json.put(Wish.CONSTANT_REVIEWCOUNT, ProductInfo.ReviewCount);
            }
            
            json.put(Wish.CONSTANT_PRODUCTRATING, ProductInfo.ProductRating);
            
            json.put(Wish.CONSTANT_REVIEWCOUNT, ProductInfo.ReviewCount);
            
        }
        catch (JSONException e)
        {
            e.printStackTrace();
        }
        return json;
        
    }
    
    /**
     * JsonArray转化为WModel list
     * @param arrays
     * @return
     */
    public static WishModel jsonArray2Wish(JSONObject json)
    {
        WishModel wishModel = new WishModel();
        if (json.optString(ServerConstant.Wish.CONSTANT_USERNAME) != JSONObject.NULL
            && !json.isNull(ServerConstant.Wish.CONSTANT_USERNAME))
        {
            wishModel.setUserName(json.optString(ServerConstant.Wish.CONSTANT_USERNAME));
        }
        wishModel.setSku(json.optInt(ServerConstant.Wish.CONSTANT_SKU));
        
        ProductInfo productInfo = new ProductInfo();
        JSONObject constant = json.optJSONObject(ServerConstant.Wish.CONSTANT_PRODUCTINFO);
        productInfo.setProductRating(constant.optDouble(ServerConstant.Wish.CONSTANT_PRODUCTRATING));
        productInfo.setListPrice(constant.optDouble(ServerConstant.Wish.CONSTANT_LISTPRICE));
        if (constant.optString(ServerConstant.Wish.CONSTANT_SHORTHEADLINE) != JSONObject.NULL
            && !constant.isNull(ServerConstant.Wish.CONSTANT_SHORTHEADLINE))
        {
            productInfo.setShortHeadLine(constant.optString(ServerConstant.Wish.CONSTANT_SHORTHEADLINE));
        }
        productInfo.setReviewCount(constant.optInt(ServerConstant.Wish.CONSTANT_REVIEWCOUNT));
        if (constant.optString(ServerConstant.Wish.CONSTANT_SLIDESHOWPICTURETHUMBURL) != JSONObject.NULL
            && !constant.isNull(ServerConstant.Wish.CONSTANT_SLIDESHOWPICTURETHUMBURL))
        {
            productInfo.setSlideShowPictureThumbURL(constant.optString(ServerConstant.Wish.CONSTANT_SLIDESHOWPICTURETHUMBURL));
        }
        if (constant.optString(ServerConstant.Wish.CONSTANT_WEBURL) != JSONObject.NULL
            && !constant.isNull(ServerConstant.Wish.CONSTANT_WEBURL))
        {
            productInfo.setWebUrl(constant.optString(ServerConstant.Wish.CONSTANT_WEBURL));
        }
        productInfo.setFreeShipping(constant.optInt(ServerConstant.Wish.CONSTANT_FREESHIPPING));
        productInfo.setPrice(constant.optDouble(ServerConstant.Wish.CONSTANT_PRICE));
        wishModel.setProductInfo(productInfo);
        return wishModel;
    }
    
    /** json 转 收藏实体
     * @param jsonString 错误的json格式将返回一个null
     * @return [参数说明]
     * @return UserModel [返回类型说明]
     * @exception throws [违例类型] [违例说明]
     * @see [类、类#方法、类#成员]
     */
    public static WishModel jsonArray2Wish(String jsonString)
    {
        if (null == jsonString || "".equals(jsonString.trim()))
        {
            return null;
        }
        
        WishModel model = null;
        try
        {
            JSONObject json = new JSONObject(jsonString);
            model = jsonArray2Wish(json);
        }
        catch (JSONException e)
        {
        }
        return model;
    }
    
    /**
     * 转换 wishmodel数组 到 productmodel数组
     * @param list
     * @return  [参数说明]
     * @return ArrayList<ProductModel> [返回类型说明]
     * @exception throws [违例类型] [违例说明]
     * @see [类、类#方法、类#成员]
     */
    public static ArrayList<ProductModel> wishModelList2pModelList(ArrayList<WishModel> wishList)
    {
        ArrayList<ProductModel> productModels = new ArrayList<ProductModel>(wishList.size());
        
        for (WishModel wishModel : wishList)
        {
            productModels.add(wishModel2pMdel(wishModel));
        }
        return productModels;
    }
    
    /**
      * 转换 WishModel 到 ProductModel
      * @param wishModel
      * @return  [参数说明]
      * @return ProductModel [返回类型说明]
      * @exception throws [违例类型] [违例说明]
      * @see [类、类#方法、类#成员]
      */
    public static ProductModel wishModel2pMdel(WishModel wishModel)
    {
        
        ProductModel productModel = null;
        if (null != wishModel)
        {
            productModel = new ProductModel();
            productModel.setSku(wishModel.getSku());
            String[] smallPictureURL = {wishModel.getProductInfo().getSlideShowPictureThumbURL()};
            productModel.setSmallPictureURL(smallPictureURL);
            productModel.setMediumPictureURL(smallPictureURL);
            productModel.setBigPictureURL(smallPictureURL);
            productModel.setPrice((float)wishModel.getProductInfo().getPrice());
            productModel.setQuantity(1);
            productModel.setShortHeadLine1(wishModel.getProductInfo().getShortHeadLine());
            productModel.setFreeShipping(wishModel.getProductInfo().getFreeShipping());
            productModel.setListprice((float)wishModel.getProductInfo().getListPrice());
            productModel.setWebUrl(wishModel.getProductInfo().getWebUrl());
        }
        return productModel;
    }
    
    public String toString()
    {
        return tojson().toString();
    }
}
